package com.handpay.zztong.hp.other.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.handpay.framework.ui.HPSignatureView;
import com.handpay.framework.ui.HPSignatureView.OnEmptyListener;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;

import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.Bill;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.paymentbill.activity.TransResultSuccessActivity;
import com.handpay.zztong.hp.protocol.ATProtocolConstant;
import com.handpay.zztong.hp.utils.AccountUtils;

/**
 * @author jmshuai
 */
public class SignatureActivity extends ZZTongActivity implements View.OnClickListener {

    private HPSignatureView mSigView = null;
    private View mContainer = null;
    private View mSaveBtn = null;
    private TextView mHints = null;
    private TextView tfName = null;
    private TextView tfMoney = null;
    private CheckBox cbPhone = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_signature);
        mContainer = findViewById(R.id.sig_all);
        mSaveBtn = this.findViewById(R.id.btn_save);
        this.findViewById(R.id.btn_save).setOnClickListener(this);
        this.findViewById(R.id.btn_clear).setOnClickListener(this);
        cbPhone = (CheckBox) this.findViewById(R.id.cbPhone);
        mHints = (TextView) this.findViewById(R.id.tv_hints);
        tfName = (TextView) this.findViewById(R.id.tfName);
        tfMoney = (TextView) this.findViewById(R.id.tfMoney);
        int mTRAD_TYPE = getIntent().getIntExtra(ACTION, Bill.TRAD_TYPE_POSPAY);
//        if (mTRAD_TYPE == Bill.TRAD_TYPE_POSPAY) {//第三方支付 没有发送短信到手机功能
//            cbPhone.setVisibility(View.VISIBLE);
//        } else {
//            cbPhone.setVisibility(View.GONE);
//        }
        cbPhone.setVisibility(View.GONE);
        tfName.setText(AccountUtils.getShopName(this));
        double amount = getIntent().getDoubleExtra(
                ATProtocolConstant.AMOUNT_KEY, 0.0d);
        tfMoney.setText(CommonUtils.formatMoney(amount / 100));
        mSigView = (HPSignatureView) this.findViewById(R.id.hpSigView);
        mSigView.setTextColor(Color.BLACK);
        mSigView.setTextSize(10);
        mSigView.setOnEmptyListener(new OnEmptyListener() {

            @Override
            public void onEmpty(boolean isEmpty) {
                if (isEmpty) {
                    mHints.setVisibility(View.VISIBLE);
                    mSaveBtn.setEnabled(false);
                } else {
                    mHints.setVisibility(View.GONE);
                    mSaveBtn.setEnabled(true);
                }
                // BUG: xiaomi2s 会导致整个界面花掉。这里强制整个界面重绘。
                mContainer.invalidate();
            }
        });
    }

    @Override
    public void onClick(View paramView) {
        int i = paramView.getId();
        if (i == R.id.btn_clear) {
            mSigView.clearText();
        } else if (i == R.id.btn_save) {
            boolean contentEmpty = mSigView.isEmpty();
            if (contentEmpty) {
                this.showAlertDialog(this, null, getString(R.string.no_signature_msg), true, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        doSave();
                        dialog.dismiss();
                    }
                });
                return;
            } else {
                doSave();
            }
        }
    }

    private void doSave() {
        Intent intent = new Intent();
        if (cbPhone.isChecked()) {
            intent.setClass(this, SignaturePhoneActivity.class);
            this.startActivityForResult(intent, TransResultSuccessActivity.REQUEST_SIGNATURE);
        } else {
            int resultCode = composeIntent(intent);
            this.setResult(resultCode, intent);
            this.finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 无论返回有没有phone，都不做处理。留在启动该activity里面处理。
        int myResultCode = composeIntent(data);
        this.setResult(myResultCode, data);
        this.finish();
        super.onActivityResult(requestCode, resultCode, data);
    }

    private int composeIntent(Intent intent) {
        if (null == intent) {
            // should never enter this!
            return Activity.RESULT_CANCELED;
        }
        Bitmap cache = mSigView.getDrawingCache();
        // 最大byte限定到100k
        byte[] cacheByte;
        // 如果getDrawCache不work，采用View.draw方法
        if (null == cache) {
            cache = Bitmap.createBitmap(mSigView.getWidth(),
                    mSigView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas cv = new Canvas(cache);
            mSigView.draw(cv);
            cacheByte = CommonUtils.resizeImage(cache, ZZTConstant.SIGMAXBYTE);
            cache.recycle();
        } else {
            cacheByte = CommonUtils.resizeImage(cache, ZZTConstant.SIGMAXBYTE);
        }
        boolean contentEmpty = mSigView.isEmpty();
        intent.putExtra("empty", contentEmpty);
        int resultCode = Activity.RESULT_OK;
        // 只有内容不为空的时候，才将result设置为OK
        if (null != cacheByte && 0 != cacheByte.length && !contentEmpty) {
            HPLog.i("Sign", "b.size:" + cacheByte.length);
            intent.putExtra("signature", cacheByte);
        } else {
            HPLog.e("Sign", "getSignature failed! contentEmpty? " + contentEmpty);
            resultCode = Activity.RESULT_CANCELED;
        }
        return resultCode;
    }

    @Override
    protected boolean isShownTopBar() {
        return false;
    }

    @Override
    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode)
            return false;
        return super.onKeyDown(keyCode, event);
    }
}
